package com.fscratch.controller;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fscratch.config.TestInfoSettings;
import com.fscratch.domain.Book;
import com.fscratch.rest.JsonResp;

/**
 * 实现 Restful HTTP 服务
 *
 */
@RestController
public class HelloWord {

    @Autowired
    TestInfoSettings testInfoSettings;

    @RequestMapping(value = "/api/v1", method = RequestMethod.GET)
    public JsonResp test() {
        Book book = new Book();
        book.setId(789L);
        book.setName("Java特种兵");
        book.setDate(new Date());
        book.setIntroduction("好东西");
        return new JsonResp().success(book);
    }

    @RequestMapping(value = "/book", method = RequestMethod.POST)
    public JsonResp createBook(@RequestBody @Valid Book book) {
        System.out.println(book.toString());
        return new JsonResp().success();
    }

    @RequestMapping(value = "/api/v1/{id}", method = RequestMethod.GET)
    public String test(@PathVariable("id") Long id) {
        return "HelloWord" + id;
    }

    // 类型安全的配置
    @RequestMapping("/showSetting")
    public JsonResp showSetting() {
        StringBuffer sb = new StringBuffer();
        sb.append(" setting name is : ").append(testInfoSettings.getName());
        sb.append(" setting age is : ").append(testInfoSettings.getAge());
        sb.append(" all is : ").append(testInfoSettings);
        return new JsonResp().success(sb);
    }

}
